<?php
namespace app\web\model;

/**
 * @package app\basic\model
 */
class UserIntegral extends Base 
{
    public function getCreateTimeAttr($v)
    {
        return date('Y-m-d',$v);
    }

    public static function change($user_id,$change_integral,$integral_type,$order_id = 0)
    {
        if($change_integral == 0) return true;
        $user = User::get($user_id);
        $after_integral = (int)$user->integral + (int)$change_integral;
        $data = [
            'user_id' => $user_id,
            'integral_type' => $integral_type,
            'order_id' => $order_id,
            'before_integral' => $user->integral,
            'change_integral' => $change_integral,
            'after_integral' => $after_integral,
            'status' => 1
        ];
        $res = self::create($data);
        if($res->isEmpty()) return false;
        $user->integral = $after_integral;
        $res = $user->save();
        return $res ? true : false;
    }

    
}